import RestaurantManagementSystem.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;

public class RestaurantService {
    private HashMap<String, Ingredient> ingredientInventory = new HashMap<>();
    private ArrayList<Meal> meals = new ArrayList<>();
    private ArrayList<RestaurantBilling> orders = new ArrayList<>();
    private ArrayList<Meal> menu = new ArrayList<>();

    public void addIngredient(String name, double price) {
        Ingredient ingredient = new Ingredient(name, price);
        ingredientInventory.put(name, ingredient);
        System.out.println("Ingredient added: " + ingredient.getName() + ", Price: " + ingredient.getPrice());
    }

    public void addSampleIngredients() {
        ingredientInventory.put("Tomato", new Ingredient("Tomato", 0.75));
        ingredientInventory.put("Lettuce", new Ingredient("Lettuce", 0.50));
        ingredientInventory.put("Cheese", new Ingredient("Cheese", 1.25));
        ingredientInventory.put("Beef Patty", new Ingredient("Beef Patty", 3.50));
        ingredientInventory.put("Chicken Breast", new Ingredient("Chicken Breast", 2.75));
        ingredientInventory.put("Bun", new Ingredient("Bun", 0.80));
        ingredientInventory.put("French Fries", new Ingredient("French Fries", 1.50));
        ingredientInventory.put("Rice", new Ingredient("Rice", 1.00));
        ingredientInventory.put("Pasta", new Ingredient("Pasta", 1.20));
        ingredientInventory.put("Marinara Sauce", new Ingredient("Marinara Sauce", 1.75));
    }

    public boolean createMeal(String mealName, ArrayList<String> ingredientNames, JFrame frame) {
        boolean flag = true;
        ArrayList<Ingredient> ingredients = new ArrayList<>();
        for (String name : ingredientNames) {
            Ingredient ingredient = ingredientInventory.get(name);
            if (ingredient != null) {
                ingredients.add(ingredient);
            } else {
                System.out.println("Ingredient not found: " + name);
                JOptionPane.showMessageDialog(frame, "Ingredient '" + name + "' not found.", "Error", JOptionPane.ERROR_MESSAGE);
                flag = false;
            }
        }
        if (!flag) {return flag;}
        Meal meal = new Meal(mealName);
        for (Ingredient ingredient : ingredients) {
            meal.addIngredient(ingredient);
        }
        meals.add(meal);
        System.out.println("Meal created: " + meal.getName() + ", Ingredients: " + meal.getIngredients() + ", Meal ID: " + meals.indexOf(meal));
        JOptionPane.showMessageDialog(frame, "Meal created: " + meal.getName() + ", Ingredients: " + meal.getIngredients() + ", Meal ID: " + meals.indexOf(meal), "Success", JOptionPane.INFORMATION_MESSAGE);
        return flag;
    }

    public boolean removeMeal(String mealName, JFrame frame) {
        for (int i = 0; i < meals.size(); i++) {
            if (meals.get(i).getName().equalsIgnoreCase(mealName)) {
                meals.remove(i);
                System.out.println("Removed meal: " + mealName);
                JOptionPane.showMessageDialog(frame, "Removed meal: " + mealName, "Success", JOptionPane.INFORMATION_MESSAGE);
                return true;
            }
        }
        System.out.println("Meal not found: " + mealName);
        JOptionPane.showMessageDialog(frame, "Meal '" + mealName + "' not found.", "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }

    public String placeOrder(HashMap<String, Integer> mealQuantities) {
        RestaurantBilling order = new RestaurantBilling();
        double totalPrice = 0;
        for (String mealName : mealQuantities.keySet()) {
            order.addMealToOrder(mealName);
        }
        orders.add(order);
        return order.displayOrder();
    }

    public double calculateBill(String orderId) {
        RestaurantBilling order = orders.get(Integer.parseInt(orderId));
        return order.calculateBill();
    }

    public HashMap<String, Ingredient> getIngredientInventory() {
        return ingredientInventory;
    }

    public ArrayList<Meal> getMeals() {
        return meals;
    }

    public void addMealToMenu(Meal meal) {
        menu.add(meal);
        System.out.println("Added '" + meal.getName() + "' to the menu.");
    }

    public ArrayList<Meal> getMenu() {
        return menu;
    }
    public void removeMealFromMenu(Meal meal) {
        menu.remove(meal);
        System.out.println("Removed '" + meal.getName() + "' from the menu.");
    }

    public int finishOrdering(JFrame frame) {
        RestaurantBilling order = new RestaurantBilling();
        for (Meal meal : menu) {
            order.addMeal(meal);
        }
        orders.add(order);
        JOptionPane.showMessageDialog(frame, order.displayOrder() + "\nYour order ID is: " + orders.indexOf(order), "Success", JOptionPane.INFORMATION_MESSAGE);
        menu.clear();
        return orders.indexOf(order);
    }

    public RestaurantBilling getOrderById(int orderId) {
        if (orderId >= 0 && orderId < orders.size()) {
            return orders.get(orderId);
        } else {
            System.out.println("Order not found: " + orderId);
            return null;
        }
    }
}
